﻿using Api.Shared.Model.Common;
using Api.Shared.Model.XinYa;
using Api.Shared.Sugar;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace FitterTest.Controllers
{
    [Route("api/private/v1/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class SugarController : ControllerBase
    {
        private readonly ISugarClientService sqliteService;
        private readonly ISugarClientService mysqlService;

        public SugarController([FromKeyedServices(nameof(SqlName.sqlite))] ISugarClientService sqliteService, [FromKeyedServices(nameof(SqlName.mysql))] ISugarClientService mysqlService)
        {
            this.sqliteService = sqliteService;
            this.mysqlService = mysqlService;
        }

        [HttpGet]
        public IActionResult CreatTable()
        {
            //service.Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(DciSendModel));
            //service.Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(DciReceiveModel));
            //service.Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(MyLogs));
            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(MenuItemNew));
            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(RolesItemNew));
            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(User));
            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(Device));
            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(MyAlarm1));
            //DeviceHis 
            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(DB91WorkorderSqlite));

            //List<DB91WorkorderSqlite> works = new();
            //for (int i = 0; i < 50; i++)
            //{
            //    DB91WorkorderSqlite work = new()
            //    {
            //        WorkOrder = $"工单号{i + 1}",
            //        ProductId = $"产品号{i}",
            //        BatchId = $"批次号{i}",
            //        ProductInfo = $"工件信息{i}",
            //        PlanTime = (short)i,
            //        Time = DateTime.Now

            //    };
            //    works.Add(work);
            //}
            //sqliteService.Db.Insertable(works).ExecuteCommand();
            //mysqlService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(MyAlarm1));

            //mysqlService.Db.CodeFirst.SetStringDefaultLength(530).InitTables(typeof(TestReport));
            //mysqlService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(TestRecord));
            mysqlService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(AbbCode));
            

            //TestRecord testRecord1 = new() { InputCurrent = 55, InputVoltage = 66.1, Number = 1, OutputCurrent = 32, OutputVoltage = 67.1, TestPC = 53.1, Time = DateTime.Now };
            //TestRecord testRecord2 = new() { InputCurrent = 555, InputVoltage = 766.1, Number = 1, OutputCurrent = 332, OutputVoltage = 637.1, TestPC = 523.5, Time = DateTime.Now };
            //List<TestRecord> testRecords = new();

            //testRecords.Add(testRecord1);
            //testRecords.Add(testRecord2);

            //TestReport testReport = new()
            //{
            //    ModelName = "t1",
            //    Operator = "o1",
            //    ProductName = "p1",
            //    ReportTime = DateTime.Now,
            //    SerialNumber = "11",
            //    TestType = 1,
            //    TestRecords = testRecords

            //};
            //var options = new JsonSerializerOptions
            //{
            //    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            //    WriteIndented = true  // 可选：使输出格式化，便于阅读
            //};
            //string str = JsonSerializer.Serialize(testReport, options);
            //mysqlService.Db.InsertNav(testReport).Include(x => x.TestRecords).ExecuteCommand();

            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(OperationRecord));
            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(WorkOrderFeedback));
            return Ok();
        }

        [HttpGet]
        public async Task<IActionResult> QureyTable()
        {
            //service.Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(DciSendModel));
            //service.Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(DciReceiveModel));
            //service.Db.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(MyLogs));
            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(MenuItemNew));
            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(RolesItemNew));
            //sqliteService.Db.CodeFirst.SetStringDefaultLength(230).InitTables(typeof(User));
            //var c =  await sqliteService.Db.Queryable<MenuItemNew>().Where(x => x.path.Contains("circle")).ToListAsync();

            // var z = await sqliteService.Db.Queryable<MenuItemNew>().Where(x => x.path.Contains("board")).ToListAsync();

            // c.ForEach(x => x.level = 12);
            // z.ForEach(x => x.level = 12);

            // await sqliteService.Db.Updateable(c).ExecuteCommandAsync();
            // await sqliteService.Db.Updateable(z).ExecuteCommandAsync();

            return Ok();
        }


        [HttpGet]
        public async Task<IActionResult> MysqlMenus()
        {
            //var a = await mysqlService.Db.Queryable<MenuItemNew>().ToListAsync();
            //var b = await mysqlService.Db.Queryable<RolesItemNew>().ToListAsync();
            //var countA = await sqliteService.Db.Insertable(a).ExecuteCommandAsync();
            //var countB = await sqliteService.Db.Insertable(b).ExecuteCommandAsync();
            //var c = await mysqlService.Db.Queryable<User>().ToListAsync();
            //var countC = sqliteService.Db.Insertable<User>(c).ExecuteCommand();

            //var d = await mysqlService.Db.Queryable<Device>().ToListAsync();
            //var countD = sqliteService.Db.Insertable(d).ExecuteCommand();

            var e = await mysqlService.Db.Queryable<MyAlarm>().OrderByDescending(x => x.Id).Take(10).ToListAsync();
            var countE = sqliteService.Db.Insertable(e).ExecuteCommand();

            return Ok(e);
        }

        [HttpPost]
        public async Task<IActionResult> AddReport([FromBody] TestReport testReport)
        {
            var c = await mysqlService.Db.InsertNav(testReport).Include(x => x.TestRecords).ExecuteCommandAsync();

            if (c)
            {
                return Ok();
            }
            else
            {
                return BadRequest("204");
            }
        }

    }
}
